<?php

class UserResource extends BaseResource
{
    public function initialize()
    {
        $this->before_filters->skip('specified_user_must_be_authenticated_user',
                                    array('only' => 'get'));
    }
    
    public function get()
    {
        // l'utilisateur est déjà chargé grâce au filtre 'must_specify_user'
        $this->responds($this->user, 200);
    }
    
    public function put()
    {
        $old_name = $this->params['username'];
        $new_name = $this->params['user']['name'];
        
        if ($old_name != $new_name) {
            try {
                $user = User::$objects->get_by_name($new_name);
                // le nouveau nom d'utilisateur est déjà pris
                $this->responds_error(409);
            } catch (SRecordNotFound $e) {}
        }
        if ($this->user->update_attributes($this->params['user'])) {
            if ($old_name != $new_name) {
                $this->response->headers['Location'] = '/api/users/'.$new_name;
                $status = 301;
            } else {
                $status = 200;
            }
            $this->responds($this->user, $status);
        } else {
            $this->responds_detailed_error($this->user->errors, 400);
        }
    }
    
    public function delete()
    {
        $this->user->delete();
        $this->render_deleted(200);
    }
}

?>